﻿using StarsAdmin.Core.Extensions;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.Core.Attributes
{
    public class LongNotDefaultValueAttribute : ValidationAttribute
    {
        protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
        {
            if (value != null && value is long && value.ToLong() == 0)
            {
                return new ValidationResult(ErrorMessage ?? "id不能为默认值。");
            }
            if (value != null && value is long[])
            {
                var longArray = value as long[];
                foreach (var item in longArray)
                {
                    if (item == 0)
                    {
                        return new ValidationResult(ErrorMessage ?? "id数组中不能存在默认值。");
                    }
                }
            }

            return ValidationResult.Success;
        }
    }
}